let animation = wx.createAnimation({
  duration: 4000, //转盘转动时间
  timingFunction: 'ease-out' //动画效果
})
const app = getApp();
Page({
  data: {
    openid: '',

    CUSTOMER_TEL: '',
    phone: '', //手机号
    rotate: 0, //度数
    turning: false, //是否点击,
    zhunsj: '', //另一个页面带来的数据
    prize: '', //中奖概率
    count: '', //剩余次数
    prizeList: [
      2330, //三等奖...
      2150, //一等奖 ...
      2630, //参与奖 ...
      2400, //二等奖  ...
      2200, //谢谢惠顾 ...
    ],
    //向1006传递的数据
    USER_PRIZE_ID: '',
    txtlist: '', //翻滚的字幕
    flag: true, //设置节流
    modalHidden: true, //新的图片模态框
    ajaj: '', //获取中奖奖项

    jiang_x: '', //奖品等级
    EMPLOYEE_ID: '',

    ad_img: '', //中奖图片
    session_key: '',
    dailinshu: '',
    CUSTOMER_ID: '',
    degnji: '',
    PRIZE_IMG: '',
  },

  back() {
    wx.navigateBack({
      delta: 1,

    })
  },

  showPopup() {


    this.setData({
      modalHidden: true,
    });

    console.log(this.data.dailinshu.USER_ASSETS_IDCARD);


    wx.request({
      url: app.globalData.interface+'/drainage/API_3005_dragonYearTwoNewCustomerRaffleALiPay',
      method: 'POST',
      data: {

        USER_ASSETS_IDCARD: this.data.dailinshu.USER_ASSETS_IDCARD,
        USER_PRIZE_ID: this.data.dailinshu.USER_PRIZE_ID,
        CUSTOMER_ID: this.data.CUSTOMER_ID,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        wx.showModal({
          title: '温馨提示',
          content: res.data.data,
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 2,
              })
            } else { //这里是点击了取消以后
              console.log('用户点击取消')
              wx.navigateBack({
                delta: 2,
              })
            }
          }
        })
      }
    })


  },















  //自定义模态框的取消
  modalCancel: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(222);
  },

  //自定义模态框的确定
  queding: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(111);
  },


  start: function () {



    let that = this
    if (that.data.flag) {


      // 节流
      that.data.flag = false
      wx.request({
        url: app.globalData.interface+'/drainage/API_3003_dragonYearTwoNewCustomerRaffle',
        method: 'POST',
        data: {
          OPEN_ID: that.data.openid,
          session_key: that.data.session_key,
          OPEN_UUID: that.data.jiami,
          USER_ASSETS_IDCARD: that.data.dailinshu.USER_ASSETS_IDCARD,
          // EMPLOYEE_ID: that.data.L4code,

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          console.log(res.data.data.VALID);

          if (res.data.result != 'success') {
            if (res.data.data.VALID == 1) {
              console.log(res.data.data.VALID);
              let degnji = res.data.data
              wx.showModal({
                title: '温馨提示',
                title: '您有未领取的红包是否领取',
                // title: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后

                    console.log(degnji);

                    if (degnji.PRIZE_ID == 1 || degnji.PRIZE_ID == 2 || degnji.PRIZE_ID == 3|| degnji.PRIZE_ID == 4) {

                      that.setData({
                        modalHidden: false,
                        ad_img: degnji.PRIZE_IMG,
                        dailinshu: degnji
                      })

                    }
                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
            } else if (res.data.data.VALID == 2) {
              wx.showModal({
                title: '温馨提示',
                title: '您已领取过',
                // title: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后

                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
            }
          } else {

            that.setData({

              prize: res.data.prize,
              dailinshu: res.data.prizeMessage,
            })

            console.log(res.data.prizeMessage.PRIZE_IMG);
            setTimeout(() => {
              that.setData({
                ad_img: res.data.prizeMessage.PRIZE_IMG
              })
            }, 5500)
            const index_ = res.data.prize;
            that.rotate(index_)
          }
        }
      })
      setTimeout(() => {
        that.data.flag = true
      }, 5500)


    }




  },

  // 转盘代码
  rotate: function (index_) {
    // rotate: function () {
    let _this = this;
    // let cat = 60;
    if (!this.data.turning) {
      let rdm = 0; //度数
      rdm = Math.floor(Math.random() * 3600); //最大10圈
      // rdm = 2100; //最大10圈
      //用索引的方法对应角度
      rdm = this.data.prizeList[index_ - 1]; //直接确定转的度数 
      // rdm =2200;//直接确定转的度数 
      animation.rotate(rdm).step();
      this.setData({
        rotate: animation.export(),
        turning: true
      });
      setTimeout(() => {
        this.setData({
          turning: false
        });
        let num = rdm % 360; //转了多少度
        //console.log(num);
        function showModal(str) {
          wx.showModal({
            title: '恭喜抽中',
            content: str,
            success: function () { //点击确定后还原到0度
              let animation = wx.createAnimation({
                duration: 100,
                timingFunction: 'ease-out'
              });
              animation.rotate(0).step();
              _this.setData({
                rotate: animation.export(),
                turning: false
              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }


        // 2330, //三等奖...
        // 2150, //一等奖 ...
        // 2630, //参与奖 ...
        // 2400, //二等奖  ...
        // 2200, //谢谢惠顾 ...






        if (rdm == 2200) {
          // showModal('谢谢惠顾');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2330) {
          // showModal('一等奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2150) {
          // showModal('三等奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2630) {
          // showModal('参与奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2400) {
          // showModal('二等奖');
          this.setData({
            modalHidden: false
          })
        }
      }, 4500)
    }
  },











  onLoad: function (e) {
    console.log(JSON.parse(e.data))
    let shuju = JSON.parse(e.data)
    console.log(shuju.data);

    console.log(shuju.data);
    this.setData({
      phone: app.globalData.phone,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,

      dailinshu: shuju.data,
    })

    console.log(this.data.phone);
    console.log(this.data.jiami);
    console.log(this.data.openid);
    console.log(this.data.session_key);


    // this.region()
  },


  onShow() {

    var that = this

    if (that.data.jiami == '' || that.data.session_key == '') {

      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求

            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              // url: 'http://192.168.3.173:8081/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  console.log(1);
                  // wx.showToast({
                  //   title: '请获取手机号',
                  //   duration: 1000
                  // })
                } else {
                  var openid = res.data.openid;
                  app.globalData.openid = openid;

                  console.log(res.data.openid);
                  that.setData({
                    openid: res.data.openid
                  })
                  // that.scanCod()

                  that.setData({
                    phone: app.globalData.phone,

                    jiami: app.globalData.jiami,
                    OPEN_ID: app.globalData.openid,
                    openid: app.globalData.openid,
                    session_key: app.globalData.session_key,
                  })
                  console.log(that.data.phone);
                  console.log(that.data.jiami);
                  console.log(that.data.openid);
                  console.log(that.data.session_key);
                }

              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {

      console.log('成功');
    }





  },




})


wx.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})